//
//  MBCommodityRightsCell.swift
//  iPhone
//
//  Created by Toj on 8/10/22.
//

import UIKit

protocol MBCommodityRightsCellDelegate: NSObjectProtocol {
    func commodityRightsCellDidTapRights(_ view: MBCommodityRightsCell)
}

private let buyButtonHeight: CGFloat = 32

// MARK: - 懒加载(属性), 变量, 初始化init
class MBCommodityRightsCell: UICollectionViewCell {
    
    weak var delegate: MBCommodityRightsCellDelegate?
    var memberRight: MemberRightsDto? { didSet {
        guard let right = memberRight else { return }
        
        let url = URL(string: right.image_url)
        imageView.kf.setImage(with: url, placeholder: "ph_star_logo".to_image)
        nameLabel.text = right.name
        
        
        if(!right.isShowed) {
            right.isShowed = true
            let event = DSEvent(category: st_className, action: "right_show", label: right.name, value: "1")
            DataStatistics.sendEvent(event: event)
        }
    }}
    
    @objc func didTapRights() {
        delegate?.commodityRightsCellDidTapRights(self)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer.init(target:self, action:#selector(didTapRights))
        addGestureRecognizer(tapGesture)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        setupMakeLayoutSubviews()
    }
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.st_cornerRadius = 8
        view.backgroundColor = "#F5F5F5".to_color
        return view
    }()
    private lazy var imageView: UIImageView = UIImageView()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 2
        view.textAlignment = .center
        view.font = UIFont.systemFont(ofSize: 12.f_scale)
        view.textColor = "#999999".to_color
        return view
    }()
}

// MARK: - 初始化
extension MBCommodityRightsCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(bgView)
        contentView.addSubview(imageView)
        contentView.addSubview(nameLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        bgView.snp.makeConstraints { make in
            make.top.left.equalTo(contentView)
            make.height.equalTo(contentView.width)
            make.width.equalTo(contentView)
        }
        
        imageView.snp.makeConstraints { make in
            make.center.equalTo(bgView)
            make.height.equalTo(48)
            make.width.equalTo(48)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(bgView.snp.bottom).offset(5)
            make.width.equalTo(bgView)
        }
    }
}
